<?php

class UserAccountLogModel
{
    private $userAccountLogKeyPrefix =   'UAL/';
    
    /**
     * 添加账号操作记录
     * @param unknown $param
     * @return boolean
     */
    public function addUserAccountLog ($uid, $action, $account, $param) {
        if (empty($uid) || !in_array($action, array_keys(AC::$accountActions)) || empty(AC::RECORD_ACCOUNT_LOG)) {
            return false;
        }
        
        $data   = $this->filterAccountLogData($uid, $action, $account, $param);
        $result = $this->insertAccountLogData($uid, $data);

        return $result;
    }
    
    /**
     * 获取账号操作列表记录
     * @param unknown $uid
     * @param unknown $id
     * @param unknown $start
     * @param unknown $length
     * @return boolean|Ambigous <unknown, boolean, multitype:>
     */
    public function getUserAccountLogs ($uid, $id, $start, $length) {
        if (empty($uid)) {
            return false;
        }
        
        $key    =   $this->userAccountLogKeyPrefix. $uid;
        $where  =   " `uid` = ".$uid;
        if ($id > 0) {
            $where = " `id` < ".$id." AND `uid` = ".$uid;
        }
        $order  =   'id DESC';
        $result =   Storage::getConditionManyData($key,$where,$order,$start,$length);
        return $result;
    }
    
    /**
     * 查看账号单个操作详情
     * @param unknown $uid
     * @param unknown $id
     * @return boolean|Ambigous <multitype:, unknown, boolean>
     */
    public function getUserAccountLogInfo ($uid, $id) {
        if (empty($uid) || empty($id)) {
            return false;
        }
        
        $key    =   $this->userAccountLogKeyPrefix. $uid;
        $where  =   array('id' => $id);
        $result =   Storage::getConditionData($key, $where);
        return  $result;
    }
    
    /**
     * 设置账号操作状态
     * @param unknown $uid
     * @param unknown $id
     * @param number $status
     * @return boolean
     */
    public function setUserAccountLogStatus ($uid, $id, $status = 0) {
        if (empty($uid) || empty($id)) {
            return false;
        }
        
        return $this->updateAccountLogData($uid, $id, array('status' => $status));
    }
    
    /**
     * 过滤账号操作记录数据
     * @param unknown $uid
     * @param unknown $action
     * @param unknown $param(type、nickname、extras)
     * @return multitype:number string NULL
     */
    private function filterAccountLogData ($uid, $action, $account, $param) {
        global $_G;
 
        $data = array(
            'uid'       =>  intval($uid),
            'account'   =>  strval($account),
            'action'    =>  strval($action),
            'status'    =>  0,
            'ip'        =>  UK::getClientIp(),
            'official'  =>  0,
            'serverhost'=>  empty($_G['serverHost']) ? "na" : $_G['serverHost'],
            'appinfo'   =>  UK::JsonFormat(AC::$appInfoList),
            'info'      =>  $this->generateAccountLogInfo($action, $param),
            'time'      =>  AC::SERVER_TIME
        );
        
        return $data;
    }
    
    /**
     * 处理操作详情数据
     * @param unknown $param
     * @return string
     */
    private function generateAccountLogInfo ($action, $param) {

        $info = array();
        switch ($action) {
            case in_array($action, array('login', 'register')):
                $info = array('nickname' => strval($param['nickname']));
                break;
            case in_array($action, array('accountbind' , 'accountunbind')):
                $info = array('account' => strval($param['account']), 'type' => strval($param['type']), 'extra' => strval($param['extra']));
                break;
        }

        $result = UK::JsonFormat($info);
        return $result;
    }
    
    /**
     * 组装返回列表数据
     * @param unknown $list
     * @return multitype:unknown number
     */
    private function assembleAccountLogsData ($list) {
        if (empty($list)) return (Object) array();
        
        $logs = array();
        foreach ($list as $value) {
            $tmp = $this->assembleAccountLogsData($item);
            if ($tmp['id'] && $tmp['uid']) $logs[] = $tmp;
        }
        
        $result = array('logs' => $logs, 'counts' => count($logs));
        return $result;
    }
    
    /**
     * 列表数据结构
     * @param unknown $data
     * @return multitype:string
     */
    public function assembleAccountLogListData ($data) {
        $result = array(
            'id'         =>  '0',
            'uid'        =>  '0',
            'actiontype' => 'login',
            'ip'         => '127.0.*.*',
            'location'   => '上海市',
            'time'       => AC::SERVER_TIME
        );
        
        UK::assembleReplacedFieldData($data, $result, array(
            'id'            =>  'id',
            'uid'           =>  'uid',
            'actiontype'    =>  'action',
            'ip'            =>  'ip',
            'time'          =>  'time'
        ));
        return $result;
    }
    
    /**
     * 内部调用写入数据
     * @param unknown $uid
     * @param unknown $param
     * @return boolean
     */
    private function insertAccountLogData ($uid, $param) {
        $key    =   $this->userAccountLogKeyPrefix . $uid;
        $result =   Storage::addData($key, $param);
        if (empty($result)) return  false;
        
        return  true;
    }
    
    /**
     * 内部调用更新数据
     * @param unknown $uid
     * @param unknown $id
     * @param unknown $param
     * @return boolean
     */
    private function updateAccountLogData ($uid, $id, $param) {
        $key    =   $this->userAccountLogKeyPrefix . $uid;
        $where  =   array('id' => $id);
        $result =   Storage::setConditionData($key, $param, $where);
        if (empty($result)) return  false;
        
        return  true;
    }
    
}

